package com.app.service.impl;

import com.app.common.SystemConstant;
import com.app.conf.RedisService;

import com.app.service.*;
import com.app.util.*;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.text.SimpleDateFormat;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @Description:验证码实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class MsgVerifyServiceImpl implements MsgVerifyService {


    @Autowired
    JdbcTemplate jdbcTemplate;


    @Autowired
    private RedisService redisService;

    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    @Override
    public int saveMsgIdentifiy(String mobile, int type) throws IOException {

        //当没有查询到记录时才进行发送
        if (redisService.getStringKey(mobile)==null) {
            int mobileCode = (int) ((Math.random() * 9 + 1) * 100000);
            SMSUtils.code(mobile,mobileCode);
            redisService.setStringKey(mobile,String.valueOf(mobileCode));
            redisService.setExpire(mobile,1, TimeUnit.MINUTES);
        } else {
            return 300;
        }
        return 200;
    }

    @Override
    public int smsReminder(String mobile,int type) {
        String uri = SystemConstant.DEFAULT_MSG_URI;//请求地址
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("uid", MSGConstant.UID);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");//格式化时间
        Timestamp data = CommonHelp.getNowTimestamp();
        String tm = sdf.format(data);
        params.add("mb", mobile);
        String pw = Md5Util.getMd5(MSGConstant.PW+tm, "UTF-8", false, 32);//32位小写 加密pw+tm
        params.add("tm", tm);
        params.add("pw", pw);
        params.add("ex", "8694");

        //1：个人求职精准匹配2：企业发布岗位精准匹配3：网格长信息录入
        if(type==1)
        {
            params.add("ms", "【肃州区就业服务平台】您好，根据您的求职意向，已为您匹配到符合要求的岗位，请访问小程序“肃州就业帮帮我”小程序或使用电脑浏览器打开https://szjy.pc.gshwsc.com查看详情，退订回T");
        }else if(type==2)
        {
            params.add("ms", "【肃州区就业服务平台】您好，根据您发布的岗位要求，已为您挑选与您岗位匹配的求职者，请访问小程序“肃州就业帮帮我”或使用电脑浏览器打开https://szjy.pc.gshwsc.com查看您心仪的求职者，退订回T");
        }else if(type==3)
        {
            params.add("ms", "【肃州区就业服务平台】您的信息已由网格长录入系统，已为您匹配符合您的岗位，请访问小程序“肃州就业帮帮我”或使用电脑浏览器打开https://szjy.pc.gshwsc.com查看您心仪的岗位，退订回T");
        }

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        // 以表单的方式提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);

        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);

        String msg = response.getBody();

        return 200;
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Timestamp data = CommonHelp.getNowTimestamp();
        String starTime = sdf.format(data);
        System.out.println(starTime);
    }


}
